package com.spsy.pay.order;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.domain.AlipayTradeQueryModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ZhifubaoUtill {

    String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQC65BWJ5yGlydMPB+THQbR/QETqHAZLkRPKW82kQVt8r930m2f/uLf+Kf7VaidVoNP72uVIEqdCXjAhX+SQVpsr5gcec+L3dcvLCvoTGuBy4HDbD9ZSzJjqob5rqml0lU4i6e7szAdN5IrLIlw83TDy4bUmQnHa/2qG5qcD+sU/xc0U7P8jrgi/C4EPgdk/0aAoUs8ztUj6jSmzfKKr0mhAnMI4UFb1gq2No71584+Uf5zvjw9juJhqkADpNQeklegaKFlcLbxbtDn7qEP0v2q/Z9CDMaS8DbhWaEac/AoEQALZkTlPIy/qjDmvSAjUV9mti3L5+eCYroZd++vXw6m7AgMBAAECggEBALQEtTaDOybtk+5bhSPlh043guoaGno5gV2fNiLsjYo6AMr30qJAtIIOw5egS8kwRGDJhab5yMCl5yiOrKPXuFn9lYfgypPglG+84Ar9DYu54xsvUxuoVfdT2w7sM+4HTx9URD81fCesZ4CpVT2PNfIONSZoRDFVa7ALhn7P06XLYNXCF+fVjlU+PoXtObeBmcV/p0nDcGrPY0V1TTu2Oa+zgwZxo3GGK/+zx9Uk87+R34i1axG8QOOeSQHXDuVlqvNnLWAAnZxlwqVuAPu8YkEkhbVQlZYRQLRnEP+CrUgKMgEAzmnONfVjMgTOFaG+XBT7juO2cWtNmHIJ0CM0tgkCgYEA/gtcieK116Rf8FOnc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Jx2v9qhuanA/rFP8XNFOz/I64IvwuBD4HZP9GgKFLPM7VI+o0ps3yiq9JoQJzCOFBW9YKtjaO9efOPlH+c748PY7iYapAA6TUHpJXoGihZXC28W7Q5+6hD9L9qv2fQgzGkvA24VmhGnPwKBEAC2ZE5TyMv6ow5r0gI1FfZrYty+fngmK6GXfvr18OpuwIDAQAB";

    public static final String OUT_TRADE_NO = "400100234567919918";

    //生成支付订单接口
    public Map pay() throws AlipayApiException {
        //public Map pay(PayData payData) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();

        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");

        alipayConfig.setAppId("2021000146657533");

        alipayConfig.setPrivateKey(privateKey);

        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);

        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");

        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();

        /*model.setOutTradeNo(payData.getCode());
        model.setTotalAmount(payData.getMoney().toString());
        model.setSubject(payData.getName());*/

        model.setOutTradeNo(OUT_TRADE_NO);

        model.setTotalAmount("0.01");//元

        model.setSubject("测试产品");

        model.setProductCode("FAST_INSTANT_TRADE_PAY");

        request.setBizModel(model);

        //后台配置文件不行
        //request.setNotifyUrl("https://71a6-61-181-211-208.ngrok-free.app/alipay");

        //回调地址
        request.setNotifyUrl("https://6c345fcf4ab0.ngrok-free.app/alipay/notify");


        AlipayTradePagePayResponse response = alipayClient.pageExecute(request);

        System.out.println("==============================");
        System.out.println(response.getBody());
        System.out.println("===============================");

        HashMap<Object, Object> map = new HashMap<>();
        if (response.isSuccess()) {
            System.out.println("调用成功");
            map.put("code",200);
            map.put("form",response.getBody());
        } else {
            map.put("code",400);
        }
        return map;
    }


    //查询接口
    public Boolean queryResult(String tradeNo) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000133628817");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(tradeNo);
        request.setBizModel(model);

        AlipayTradeQueryResponse response = alipayClient.execute(request);
        System.out.println(response.getBody());
        return response.isSuccess();
    }


    public static void main(String[] args) {
        try {
            ZhifubaoUtill utill = new ZhifubaoUtill();
            utill.pay();
            //utill.queryResult(OUT_TRADE_NO);
            Thread.sleep(1000);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

}
